<?php

/*
	存放一些公共调用、和不需要登录验证的接口
*/

namespace api\modules\v1\controllers;

use Yii;
use api\modules\models\member\Member;
use common\utils\ChuangnanSms;
use yii\db\Query;
use common\models\DB;
use common\models\SmsModel;
use common\utils\FileUpload;
use crazyfd\qiniu\Qiniu;
use common\utils\CommonFun;

class CommonController extends CoreController
{

	public function actionTest()
	{
		//触发后续
		$params = array();
		$params['money'] = $conf['value'];
		$params['member_id'] = $this->_memberId;
		$params['source_id'] = $orderId;
		$params['mch_id'] = MCHID;
		$params['trigger_type'] = 1;
		$params['desc'] = '会员投资';
		BaseBonus::bonusRun($params);
	}

	//通过id或者电话获取用户
	public function actionGetmember()
	{
		$member = Member::getMemberByIdOrMobile($this->request['member']);
		if($member) $this->out('用户信息',$member);
		$this->error('无此人信息');
	}


	/*
		* 发送短信验证码
		* mobile  手机号
		* type   短信类型  1：注册验证码 2修密码
	*/
	public function actionSendsms()
	{
		if(!$mobile = $this->request['mobile'])
			$this->error('手机号不能为空');
		$type = $this->request['type']?$this->request['type']:1;
		$code = rand(100000,999999);
		$msg  = '';
		$memberId = 0;
		if($type=='1')
			$msg = '您好，你的验证码为：'.$code.'，请不要轻易告诉别人！';

		$smsObj = new ChuangnanSms;
		$res    = $smsObj->sendSMS($mobile,$msg);
		$res    = json_decode($res,1);
		//添加日志
		SmsModel::addSmsLog($mobile,$code,$msg,$res,$type,$memberId);	
		if(!$res['code'])
		{
			//发送成功
			SmsModel::updateMemberSms($mobile,$code,$memberId); 
			$this->out('发送成功');
		}else
		{
			$this->error($res['errorMsg']); 
		}
	}


	
	/*
		## 发送邮箱验证码
		* member_email  手机号
		* type   短信类型  1：验证码
	*/	
	public function actionSend_email()
	{
		if(!$email = $this->request['member_email']) $this->error('邮箱不能为空');
		$type = $this->request['type']?$this->request['type']:1;
		$code = rand(100000,999999);
		$memberId = 0;
		$nickname = $email;
        if($type != 1)
        {
            $member = Member::find()->select(['member_id','member_name'])->where(["member_email"=>$email])->asarray()->one();
            if(!$member) $this->error('此邮箱未注册'); 
            $nickname = $member['member_name'];           
        }

        $msg  = '';
		$appName = Yii::$app->params['appName'];
        if($type == '1')
        {
            $msg = '<p>尊敬的   '.$nickname.'用户，您好！</p>';
            $msg .= '<p style="text-indent:2em;">您在'.date("Y-m-d H:i:s").'注册成为LCL的会员，您的验证码为：'.$code.'。</p>';
            $msg .= '<p>您收到这封邮件，是由于在LCL进行了 用户邮箱验证使用了这个邮箱地址。如果您并没有访问过【'.$appName.'】，或没有进行上述操作，请忽 略这封邮件</p>';
            $title = '注册验证码';
        }elseif ($type == '2')
        {
            $msg = '<h2>尊敬的 '.$nickname.'用户，您好！</h2>';
            $msg .= '<p>	您在'.date("Y-m-d H:i:s").'修改密码，你的验证码为：'.$code.'。该验证码非常重要，请勿向任何人包括客服提供验证码。</p>';
            $title = '安全验证';
        }
        $msg .= '<p style="text-indent:2em;">如需了解更多信息，请登录APP了解 下载地址<a style="color:#000" href="http://'.$_SERVER['HTTP_HOST'].'/app/download.html">aap下载</a></p>'; 
        $msg .= "<br/>"; 
        // $msg .= '<p>【'.$appName.'】--营运中心</p>';
        $msg .= '<p style="background:#409EFF;line-height:40px;text-indent:20px;color:#fff;">温馨提示</p>';
        $msg .= "<p>（1）请不要直接回复此邮件，如有疑问请联系我们的客服,微信：wbsts8090</p>"; 
        $msg .= "<p>（2）为确保您能正常及时收到我们的邮件，请把service@topwine.33th.me 加入到你的邮箱白名单中，谢谢您的支持</p>";
		$res = CommonFun::sendMailer($email,$msg,$title);
		//添加日志
		SmsModel::addSmsLog($email,$code,$msg,$res,$type,$memberId);	
		if($res)
		{
			//发送成功
			SmsModel::updateMemberSms($email,$code,$memberId); 
			$this->out('发送成功');
		}else
		{
			$this->error($res['errorMsg']); 
		}
	}	



	/*
		*同意协议
	*/
	public function actionAgreement() 
	{
		$html = '<p>同意注册，后果自行承担</p>';
		$title = '注册协议';
		$this->out('协议',array('content'=>$html,'title'=>$title));
	}


	//图片上传
	public function actionUpload() 
	{
		$qiNiuOpen 	= Yii::$app->params['qiniu']['open'];
		if($qiNiuOpen)
			$this->qinuiUpload();
		else
			$this->fileUpload();
	}

	//本地上传
	private function fileUpload()
	{
		$basePath = $_SERVER['DOCUMENT_ROOT'];
		$filePath = $this->getFilePath($this->request('uptype'));
		
		$upload = new FileUpload();
		$res = $upload->upload('file',$basePath.'/'.$filePath);
		if($res){
			$fileName = $upload->getFileName();
			$data = array('url'=>'http://'.$_SERVER['HTTP_HOST'].'/'.$filePath.'/'.$fileName);
			$this->out('上传成功',$data);
		}
		$this->error('上传失败');		
	}

	//七牛上传
	private function qinuiUpload()
	{
		$qiNiuConf 	= Yii::$app->params['qiniu'];
		$ak 		= $qiNiuConf['ak'];
		$sk 		= $qiNiuConf['sk'];
		$domain 	= $qiNiuConf['domain'];
		$bucket 	= $qiNiuConf['bucket'];
		$zone 		= $qiNiuConf['zone'];	
		$qiniu 		= new Qiniu($ak, $sk,$domain, $bucket,$zone);
		$key 		= time();
		$key 		.= strtolower(strrchr($_FILES['file']['name'], '.'));
		$filePath 	= $this->getFilePath($this->request('uptype'));
		$key 		= $filePath.$key;

		$res = $qiniu->uploadFile($_FILES['file']['tmp_name'],$key);
		$data = array('url'=>'http://'.$domain.'/'.$key); 
		$this->out('上传结果:'.json_encode($res),$data);
	}

	//图片存放路径
	private function getFilePath($upType)
	{
		switch ($upType) {
			case '1':
				$filePath = 'upload/avatar';	
				break;
			
			default:
				$filePath = 'upload/avatar';	
				break;
		}
		return $filePath;
			
	}




}

